<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" charset="utf-8">
var admin_iconcls_datagrid;
var admin_iconcls_editRow = undefined;
var admin_iconcls_addRow = undefined;
$(function(){

	  admin_iconcls_datagrid=$('#admin_iconcls_datagrid').datagrid({
		url : '${pageContext.request.contextPath}/iconClsAction!dataGrid.action',
		title:'图标信息管理',
		iconCls:'icon-tip',
		toolbar:'#admin_iconcls_toolbar',
		fit : true,
		border : false,
		striped:true,
		idField : 'id',
		rownumbers: true,		//显示行号
		fitColumns : true,		//显示横向滚动条
		pagination : true,
		pageSize : 20,
		pageList : [ 10, 20, 30, 40, 50 ],
		sortName:'cjsj',
		sortOrder:'asc',
		columns : [[{
					title:'编号',
					field:'id',
					width:100,
					checkbox : true,
				},{
					title : '菜单图标',
					field : 'iconCls',
					width : 100,
					sortable:true,
					editor : {
					type : 'combobox',
					options : {
						valueField : 'iconCls',
						textField : 'iconCls',
						panelHeight : '200',
						url:'${pageContext.request.contextPath}/iconClsAction!comboboxInfo.action',
						formatter : function(v) {
							return sy.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>{1}', v.iconCls, v.text);
						}
					}
				}
				},{
					title:'图标名称',
					field:'text',
					width:100,
					sortable:true,
					editor : {
					type : 'text',
					}
					
				},{
					title:'创建时间',
					field:'cjsj',
					width:100,
					sortable:true,
					
				}]],
		
			onAfterEdit : function(rowIndex, rowData, changes) {
				var inserted = admin_iconcls_datagrid.datagrid('getChanges', 'inserted');
				var updated = admin_iconcls_datagrid.datagrid('getChanges', 'updated');
				if (inserted.length < 1 && updated.length < 1) {
					admin_iconcls_editRow = undefined;
					admin_iconcls_addRow = undefined;
					admin_iconcls_datagrid.datagrid('unselectAll');
					return;
				}
				var url = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/iconClsAction!add.action';
				}
				if (updated.length > 0) {
					url = '${pageContext.request.contextPath}/iconClsAction!edit.action';
				}
				$.ajax({
					url : url,
					data : rowData,
					dataType : 'json',
					type:'POST',
					success : function(r) {
						if (r.success) {
							admin_iconcls_datagrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : '成功'
							});
							admin_iconcls_editRow = undefined;
							admin_iconcls_addRow = undefined;
							admin_iconcls_datagrid.datagrid('load');
						} else {
							admin_iconcls_datagrid.datagrid('rejectChanges');
							$.messager.alert('错误', r.msg, 'error');
						}
						admin_iconcls_datagrid.datagrid('unselectAll');
						admin_iconcls_editRow = undefined;
						admin_iconcls_addRow = undefined;
					}
				});
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#admin_iconcls_easyui-menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			},
			onDblClickRow : function(rowIndex, rowData) {
				if(admin_iconcls_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					
					if (admin_iconcls_editRow != undefined) {
						admin_iconcls_datagrid.datagrid('endEdit', admin_iconcls_editRow);
					}

					if (admin_iconcls_editRow == undefined) {
						admin_iconcls_datagrid.datagrid('beginEdit', rowIndex);
						admin_iconcls_editRow = rowIndex;
						admin_iconcls_datagrid.datagrid('unselectAll');
					}
				}
				
			}
		
		
			});
			
		});
		function admin_iconcls_appendColumns(){
			
			if (admin_iconcls_editRow != undefined||admin_iconcls_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
		
			else{
				admin_iconcls_datagrid.datagrid('unselectAll');
		
		
				var row = {};
				admin_iconcls_datagrid.datagrid('insertRow', {
					index : 0,
					row : row
				});
				admin_iconcls_addRow = 0;
				admin_iconcls_datagrid.datagrid('selectRow', admin_iconcls_addRow);
				admin_iconcls_datagrid.datagrid('beginEdit', admin_iconcls_addRow);
			}
		}

		function admin_iconcls_edit() {
			var rows = admin_iconcls_datagrid.datagrid('getSelections');
			if (rows.length == 1) {
				if(admin_iconcls_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					if (admin_iconcls_editRow != undefined) {
						admin_iconcls_datagrid.datagrid('endEdit', admin_iconcls_editRow);
					}
					if (admin_iconcls_editRow == undefined) {
						admin_iconcls_editRow = admin_iconcls_datagrid.datagrid('getRowIndex', rows[0]);
						admin_iconcls_datagrid.datagrid('beginEdit', admin_iconcls_editRow);
						admin_iconcls_datagrid.datagrid('unselectAll');
					}
				}
				
			} else {
				$.messager.show({
					msg : '请选择一项进行修改！',
					title : '错误'
				});
			}
		}
		function admin_iconcls_del() {
			if (admin_iconcls_editRow != undefined||admin_iconcls_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
			var rows = admin_iconcls_datagrid.datagrid('getSelections');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/iconClsAction!delete.action',
							data : {
								ids : ids.join(',')
							},
							type:'POST',
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									admin_iconcls_datagrid.datagrid('acceptChanges');
									$.messager.show({
										msg : r.msg,
										title : '成功'
									});
									admin_iconcls_editRow = undefined;
									admin_iconcls_addRow = undefined;
									admin_iconcls_datagrid.datagrid('clearSelections');
									admin_iconcls_datagrid.datagrid('load');
								} else {
									admin_iconcls_datagrid.datagrid('rejectChanges');
									$.messager.alert('错误', r.msg, 'error');
								}
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！', 'error');
			}
		}
		function admin_iconcls_save(){
			if (admin_iconcls_editRow != undefined) {
				admin_iconcls_datagrid.datagrid('endEdit', admin_iconcls_editRow);
				admin_iconcls_editRow = undefined;
			}
			if (admin_iconcls_addRow != undefined) {
				admin_iconcls_datagrid.datagrid('endEdit', admin_iconcls_addRow);
				admin_iconcls_datagrid.datagrid('getChanges', 'inserted');
			}
		}
		function admin_iconcls_unedit(){
			admin_iconcls_datagrid.datagrid('unselectAll');
			admin_iconcls_datagrid.datagrid('rejectChanges');
			admin_iconcls_editRow = undefined;
			admin_iconcls_addRow = undefined;
		}
</script>
<div id="admin_iconcls_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false">
		<div id="admin_iconcls_toolbar" class="datagrid-toolbar" style="height: auto;display: none;">
			<div>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="admin_iconcls_appendColumns();" plain="true" href="javascript:void(0);">增加</a> 
				<a class="easyui-linkbutton" iconCls="icon-remove" onclick="admin_iconcls_del();" plain="true" href="javascript:void(0);">删除</a> 
				<a class="easyui-linkbutton" iconCls="icon-edit" onclick="admin_iconcls_edit();" plain="true" href="javascript:void(0);">编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-save" onclick="admin_iconcls_save();" plain="true" href="javascript:void(0);">保存</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_iconcls_unedit();" plain="true" href="javascript:void(0);">取消编辑</a> 
				<a class="easyui-linkbutton" iconCls="icon-undo" onclick="admin_iconcls_datagrid.datagrid('unselectAll');" plain="true" href="javascript:void(0);">取消选中</a>
			</div>
		</div>
		<table id="admin_iconcls_datagrid"></table>
	</div>
</div>